//
//  LSReadSenView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/11/1.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSReadSenView.h"

@implementation LSReadSenView {
    NSTimer *_timer;
    CGFloat _second;
    NSInteger _totalTime;
    TYAttributedLabel *_currentLabel;
}

- (instancetype)initWithFrame:(CGRect)frame WithSenArr:(NSArray *)senarr{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        CGFloat height = 0;

        for (NSInteger i = 0; i<senarr.count; i++) {
            
            NSString *text = [NSString stringWithFormat:@"%ld.%@",i+1,senarr[i]];
            
            TYAttributedLabel *senLabel = [[TYAttributedLabel alloc] initWithFrame:CGRectMake(12,height, kWidth-24,10)];
            //senLabel.delegate = self;
            senLabel.tag = 3000+i;
            senLabel.attributedText = [text dr_changeCharSpace:1.5 LineSpace:5 Font:17];//[text dr_HtmlStringWithFontSize:17];
            senLabel.numberOfLines = 0;
            senLabel.lineBreakMode = NSLineBreakByWordWrapping;
            senLabel.linesSpacing = 5;
            [senLabel sizeToFit];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [senLabel addGestureRecognizer:tap];
            
            height = height+senLabel.height+10;
            [self addSubview:senLabel];
        }
        
        self.height = height+200;
    }
    
    return self;
}

- (void)tapAction:(UIGestureRecognizer*)gesture {
    
    TYAttributedLabel *attributedLabel = (TYAttributedLabel*)gesture.view;
    _currentLabel = attributedLabel;
    self.userInteractionEnabled = NO;
    
    NSRange range = NSMakeRange(0, attributedLabel.attributedText.length);
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:attributedLabel.attributedText];
    [attr addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:range];
    attributedLabel.attributedText = attr;
    
    _totalTime = [attributedLabel.attributedText.string componentsSeparatedByString:@" "].count / _readRate;
    _second = 0;
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeAction:) userInfo:attributedLabel repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(LSReadSenTest:)]) {
        
        [self.delegate LSReadSenTest:attributedLabel.attributedText.string];
    }
    
    
}


- (void)timeAction:(NSTimer*)time {
    
    TYAttributedLabel *senLabel = time.userInfo;
    _second = _second + 0.1;
    CGFloat len = (_second/_totalTime) * senLabel.attributedText.length;
    if (len >= senLabel.attributedText.length) {
        len = senLabel.attributedText.length;
    }
    NSRange range = NSMakeRange(0, len);
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    long number = 1.5;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    NSMutableAttributedString *mutattr = [[NSMutableAttributedString alloc] initWithString:senLabel.attributedText.string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],NSParagraphStyleAttributeName:paragraphStyle}];
    [mutattr addAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} range:range];
    [mutattr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[mutattr length])];
    
    senLabel.attributedText = mutattr;
    
    if (_second>=_totalTime+2) {
        
        //结束录音，代理回调
        [_timer invalidate];
        _timer = nil;
        self.userInteractionEnabled = YES;
        
       // [senLabel addLinkWithLinkData:senLabel.attributedText.string linkColor:[UIColor orangeColor] underLineStyle:kCTUnderlineStyleNone range:NSMakeRange(0,senLabel.attributedText.length)];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(LSReadSenEnd)]) {
            [self.delegate LSReadSenEnd];
        }
        
    }
}

- (void)stop {
    [_timer invalidate];
    _timer = nil;
}

- (void)pause {
    [_timer setFireDate:[NSDate distantFuture]];
}

- (void)resume {
    [_timer setFireDate:[NSDate distantPast]];
    [_timer invalidate];
    _timer = nil;
    
    NSRange range = NSMakeRange(0, _currentLabel.attributedText.length);
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:_currentLabel.attributedText];
    [attr addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:range];
    _currentLabel.attributedText = attr;
    self.userInteractionEnabled = YES;
}


@end
